/*******************************************************************/
/* @ All Rights Reserved Copyright (C) 2014 broada,Ltd. 		   */
/*																   */
/*	NAME	  = t__sqlserver_bigint_add.c						   */
/*	NOTE	  = 												   */
/*	DATE	  = 2014/11/25 by geyh								   */
/*******************************************************************/

#include <stdio.h>
#include <string.h>
#include <CUnit/Basic.h>

#include <postgresql/config.h>
#include <postgresql/postgresql_core.h>
#include <postgresql/postgresql_comm.h>

static int __suite_init(void)
{

    return 0;
}

static int __suite_cleanup(void)
{

    return 0;
}

static void __case1(void)
{
    int i_ret = FR_SUCC;
    int i_packet_len = 0;
    Frbyte_Packet *p_st_data = NULL;
    Sql_field_bind *p_st_bind = NULL;
    p_st_bind = (Sql_field_bind *) frmalloc(sizeof(Sql_field_bind));
    memset(p_st_bind, 0x0, sizeof(Sql_field_bind));
    Frdata_field *p_st_value = NULL;
    Frlist_node *p_st_node = NULL;

    p_st_data = bytebuff_creat();
    bytebuff_add(p_st_data, (char *) "123456789", 5);

    i_ret = psotgresql_get_bind(p_st_data, &i_packet_len, -1, POSTGRESQL_TEXT, p_st_bind);

    p_st_node = FRLIST_FIRST_NODE(&p_st_bind->st_field_list);
    p_st_value = (Frdata_field *) FRLIST_GET_NODE_DATA(p_st_node);
    CU_ASSERT_EQUAL(i_ret, FR_SUCC);
    CU_ASSERT_PTR_NULL(p_st_value->p_field_value);
    //CU_ASSERT_STRING_EQUAL(p_st_value->p_field_value, "\x01\x01\x01");

    frdb_field_bind_free(&p_st_bind);
    bytebuff_free(&p_st_data);

    return;
}

static void __case2(void)
{
    int i_ret = FR_SUCC;
    int i_packet_len = 0;
    Frbyte_Packet *p_st_data = NULL;
    Sql_field_bind *p_st_bind = NULL;
    p_st_bind = (Sql_field_bind *) frmalloc(sizeof(Sql_field_bind));
    memset(p_st_bind, 0x0, sizeof(Sql_field_bind));
    Frdata_field *p_st_value = NULL;
    Frlist_node *p_st_node = NULL;

    p_st_data = bytebuff_creat();
    bytebuff_add(p_st_data, (char *) "123456789", 5);

    i_ret = psotgresql_get_bind(p_st_data, &i_packet_len, 0, POSTGRESQL_TEXT, p_st_bind);

    p_st_node = FRLIST_FIRST_NODE(&p_st_bind->st_field_list);
    p_st_value = (Frdata_field *) FRLIST_GET_NODE_DATA(p_st_node);
    CU_ASSERT_EQUAL(i_ret, FR_SUCC);
    CU_ASSERT_PTR_NULL(p_st_value->p_field_value);
    //CU_ASSERT_STRING_EQUAL(p_st_value->p_field_value, "\x01\x01\x01");

    frdb_field_bind_free(&p_st_bind);
    bytebuff_free(&p_st_data);

    return;
}

static void __case3(void)
{
    int i_ret = FR_SUCC;
    int i_packet_len = 0;
    Frbyte_Packet *p_st_data = NULL;
    Sql_field_bind *p_st_bind = NULL;
    p_st_bind = (Sql_field_bind *) frmalloc(sizeof(Sql_field_bind));
    memset(p_st_bind, 0x0, sizeof(Sql_field_bind));

    p_st_data = bytebuff_creat();
    bytebuff_add(p_st_data, (char *) "123456789", 5);

    i_ret = psotgresql_get_bind(p_st_data, &i_packet_len, 4, POSTGRESQL_TEXT, p_st_bind);

    CU_ASSERT_EQUAL(i_ret, FR_FAIL);

    frfree(p_st_bind);
    bytebuff_free(&p_st_data);

    return;
}

static void __case4(void)
{
    int i_ret = FR_SUCC;
    int i_packet_len = 3;
    Frbyte_Packet *p_st_data = NULL;
    Sql_field_bind *p_st_bind = NULL;
    p_st_bind = (Sql_field_bind *) frmalloc(sizeof(Sql_field_bind));
    memset(p_st_bind, 0x0, sizeof(Sql_field_bind));

    p_st_data = bytebuff_creat();
    bytebuff_add(p_st_data, (char *) "123456789", 5);

    i_ret = psotgresql_get_bind(p_st_data, &i_packet_len, 4, POSTGRESQL_TEXT, p_st_bind);

    CU_ASSERT_EQUAL(i_ret, FR_FAIL);

    frfree(p_st_bind);
    bytebuff_free(&p_st_data);

    return;
}

static void __case5(void)
{
    int i_ret = FR_SUCC;
    int i_packet_len = 5;
    Frbyte_Packet *p_st_data = NULL;
    Sql_field_bind *p_st_bind = NULL;
    p_st_bind = (Sql_field_bind *) frmalloc(sizeof(Sql_field_bind));
    memset(p_st_bind, 0x0, sizeof(Sql_field_bind));
    Frdata_field *p_st_value = NULL;
    Frlist_node *p_st_node = NULL;

    p_st_data = bytebuff_creat();
    bytebuff_add(p_st_data, (char *) "123456789", 5);

    i_ret = psotgresql_get_bind(p_st_data, &i_packet_len, 4, POSTGRESQL_TEXT, p_st_bind);

    p_st_node = FRLIST_FIRST_NODE(&p_st_bind->st_field_list);
    p_st_value = (Frdata_field *) FRLIST_GET_NODE_DATA(p_st_node);
    CU_ASSERT_EQUAL(i_ret, FR_SUCC);
    CU_ASSERT_STRING_EQUAL(p_st_value->p_field_value, "1234");

    frdb_field_bind_free(&p_st_bind);
    bytebuff_free(&p_st_data);

    return;
}

static void __case6(void)
{
    int i_ret = FR_SUCC;
    int i_packet_len = 5;
    Frbyte_Packet *p_st_data = NULL;
    Sql_field_bind *p_st_bind = NULL;
    p_st_bind = (Sql_field_bind *) frmalloc(sizeof(Sql_field_bind));
    memset(p_st_bind, 0x0, sizeof(Sql_field_bind));
    Frdata_field *p_st_value = NULL;
    Frlist_node *p_st_node = NULL;

    p_st_data = bytebuff_creat();
    bytebuff_add(p_st_data, (char *) "123456789", 5);

    i_ret = psotgresql_get_bind(p_st_data, &i_packet_len, 4, POSTGRESQL_BINARY, p_st_bind);

    p_st_node = FRLIST_FIRST_NODE(&p_st_bind->st_field_list);
    p_st_value = (Frdata_field *) FRLIST_GET_NODE_DATA(p_st_node);
    CU_ASSERT_EQUAL(i_ret, FR_SUCC);
    CU_ASSERT_STRING_EQUAL(p_st_value->p_field_value, "BINARY");

    frdb_field_bind_free(&p_st_bind);
    bytebuff_free(&p_st_data);

    return;
}

int add_suite_psotgresql_get_bind(void)
{
    CU_pSuite pSuite = NULL;

    if ((pSuite = CU_add_suite(MODULE_NAME" psotgresql_get_bind()", __suite_init, __suite_cleanup)) == NULL)
        return FR_FAIL;

    //增加测试用例
    if (CU_add_test(pSuite, "case1", __case1) == NULL ||
        CU_add_test(pSuite, "case2", __case2) == NULL ||
        CU_add_test(pSuite, "case3", __case3) == NULL ||
        CU_add_test(pSuite, "case4", __case4) == NULL ||
        CU_add_test(pSuite, "case5", __case5) == NULL ||
        CU_add_test(pSuite, "case6", __case6) == NULL)
        return FR_FAIL;

    return FR_SUCC;
}
